package com.ruoyi.web.controller.wechat;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.wechat.domain.WechatUser;
import com.ruoyi.wechat.domain.WechatUserPointsRecord;
import com.ruoyi.wechat.service.IWechatUserService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 微信用户Controller
 *
 * @author KevenPotter
 * @date 2024-04-26 23:59:59
 */
@RestController
@RequestMapping("/wechat/user")
@AllArgsConstructor
public class WechatUserController extends BaseController {

    private final IWechatUserService wechatUserService;
    private final TokenService tokenService;

    /**
     * 查询微信用户列表
     */
    @PreAuthorize("@ss.hasPermi('wechat:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(WechatUser wechatUser) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        startPage();
        List<WechatUser> list = wechatUserService.getList(wechatUser);
        list.forEach(item -> wechatUserService.filterOutbound(item, loginUser));
        return getDataTable(list);
    }

    /**
     * 导出微信用户列表
     */
    @PreAuthorize("@ss.hasPermi('wechat:user:export')")
    @Log(title = "微信用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WechatUser wechatUser) {
        List<WechatUser> list = wechatUserService.getList(wechatUser);
        ExcelUtil<WechatUser> util = new ExcelUtil<WechatUser>(WechatUser.class);
        util.exportExcel(response, list, "微信用户数据");
    }

    /**
     * 获取微信用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('wechat:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        WechatUser wechatUser = wechatUserService.getById(id);
        wechatUser = wechatUserService.filterOutbound(wechatUser, loginUser);
        return success(wechatUser);
    }

    /**
     * 新增微信用户
     */
    @PreAuthorize("@ss.hasPermi('wechat:user:add')")
    @Log(title = "微信用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WechatUser wechatUser) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        wechatUser = wechatUserService.filterInbound(wechatUser, loginUser);
        return toAjax(wechatUserService.insertWechatUser(wechatUser));
    }

    /**
     * 修改微信用户
     */
    @PreAuthorize("@ss.hasPermi('wechat:user:edit')")
    @Log(title = "微信用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WechatUser wechatUser) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        wechatUser = wechatUserService.filterInbound(wechatUser, loginUser);
        return toAjax(wechatUserService.updateWechatUser(wechatUser));
    }

    /**
     * 删除微信用户
     */
    @PreAuthorize("@ss.hasPermi('wechat:user:remove')")
    @Log(title = "微信用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(wechatUserService.deleteWechatUserByIds(ids));
    }

    /**
     * 查询微信用户字典
     *
     * @return 返回微信用户字典
     * @author KevenPotter
     * @date 2024-04-29 15:53:20
     */
    @GetMapping("/getWechatUserDicts")
    public AjaxResult getWechatUserDicts() {
        List<WechatUser> allList = wechatUserService.getAllList();
        List<SysDictData> wechatUserDictList = new ArrayList<>();

        SysDictData defaultData = new SysDictData();
        defaultData.setDictLabel("当前任务");
        defaultData.setDictValue("0");
        wechatUserDictList.add(defaultData);

        allList.forEach(item -> {
            SysDictData sysDictData = new SysDictData();
            sysDictData.setDictLabel(item.getNickName());
            sysDictData.setDictValue(String.valueOf(item.getId()));
            wechatUserDictList.add(sysDictData);
        });
        return AjaxResult.success(wechatUserDictList);
    }

    /**
     * 查询微信用户字典(无系统)
     *
     * @return 返回微信用户字典(无系统)
     * @author KevenPotter
     * @date 2024-05-02 19:49:20
     */
    @GetMapping("/getWechatUserDictsWithoutSystem")
    public AjaxResult getWechatUserDictsWithoutSystem() {
        List<WechatUser> allList = wechatUserService.getAllList();
        List<SysDictData> wechatUserDictList = new ArrayList<>();
        allList.forEach(item -> {
            SysDictData sysDictData = new SysDictData();
            sysDictData.setDictLabel(item.getNickName());
            sysDictData.setDictValue(String.valueOf(item.getId()));
            wechatUserDictList.add(sysDictData);
        });
        return AjaxResult.success(wechatUserDictList);
    }

    /**
     * 修改微信用户积分
     *
     * @author KevenPotter
     * @date 2024-05-02 23:38:16
     */
    @PreAuthorize("@ss.hasPermi('wechat:user:points')")
    @Log(title = "微信用户", businessType = BusinessType.UPDATE)
    @PutMapping("/setPoints")
    public AjaxResult setPoints(@RequestBody WechatUserPointsRecord wechatUserPointsRecord) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        return toAjax(wechatUserService.dispatchPoints(wechatUserPointsRecord, loginUser));
    }
}
